package gui.server.listener;

import gui.ButtonName;
import gui.GuiCommon;
import gui.common.CommonListener;
import gui.common.LoginDialogue;
import gui.common.MessageListPanel;
import gui.common.detail.DetailPage;
import gui.common.detail.ForumDetail;
import gui.common.detail.GroupDetail;
import gui.common.detail.MessageDetail;
import gui.common.msg.MsgType;
import gui.common.msg.SysMessage;
import gui.server.GroupAdminList;
import gui.server.Server;
import gui.server.UserListPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import data.Forum;
import data.Group;
import data.Message;
import data.User;


public class ServerButtonListener  implements ActionListener{
	private DetailPage src=null;
	public ServerButtonListener(DetailPage source) {
		this.src=source;
	}
	public void actionPerformed(ActionEvent e) {
		Server svr=Server.getInstance();
		String buttonName = e.getActionCommand();
		System.out.println(buttonName);
		if (buttonName.equals(ButtonName.LOGIN)){
        	if(svr.isServerStart()){
	        	LoginDialogue ld=new LoginDialogue(svr);
	        	ld.setVisible(true);
        	}
        }else if (buttonName.equals(ButtonName.SAVE)){
        	if(svr.isServerStart()){
        		GroupAdminList ga=(GroupAdminList)svr.getContent();
	        	String welcome=ga.getWeclomeInfo();
	        	SysMessage msg=new SysMessage();
	        	msg.subject="Welcome info";
	        	try {
	        		svr.getDataClient().getRemoteServer().setWelcomeInfo(welcome);
	        		msg.detail="Welcome info saved";
	        		msg.type=MsgType.Success;
				} catch (Exception e2) {
					e2.printStackTrace();
					msg.detail=e2.getMessage();
	        		msg.type=MsgType.Error;
				}
				GuiCommon.showAlertWindow(msg);
	        }
        }else if(buttonName.equals(ButtonName.UPDATE_GROUP)){
        	GroupAdminList gal=(GroupAdminList)svr.getContent();
        	Group group=gal.getSelectedGroup();
        	SysMessage msg=new SysMessage();
        	msg.subject="Group Manangerment";
        	if(group==null){
        		msg.detail="Please select a group first";
        	}else{
        		try {
        			msg=svr.getDataClient().getRemoteServer().updateGroup(group);
        			msg.type=MsgType.Success;
        			msg.detail="Group update success";
        			ServerMenuEventListenr.showGroupList(true);
				} catch (Exception e2) {
					e2.printStackTrace();
				}
        		
        	}
        	GuiCommon.showAlertWindow(msg);
        }else if(buttonName.equals(ButtonName.DELETE_GROUP)){
        	GroupAdminList gal=(GroupAdminList)svr.getContent();
        	Group group=gal.getSelectedGroup();
        	SysMessage msg=new SysMessage();
        	msg.subject="Group Manangerment";
        	if(group==null){
        		msg.detail="Please select a group first";
        	}else{
        		try {
        			msg=svr.getDataClient().getRemoteServer().deleteGroup(group);
        			msg.type=MsgType.Success;
        			msg.detail="Group deleted";
        			ServerMenuEventListenr.showGroupList(true);
				} catch (Exception e2) {
					e2.printStackTrace();
				}
        	}
        	GuiCommon.showAlertWindow(msg);
        }else if(buttonName.equals(ButtonName.UPDATE_USER)){
        	UserListPanel gal=(UserListPanel)svr.getContent();
        	User user=gal.getCurrentUser();
        	System.out.println(user.getState());
        	SysMessage msg=new SysMessage();
        	msg.subject="User Manangerment";
        	if(user==null){
        		msg.detail="Please select a user first";
        	}else{
        		try {
        			msg=svr.getDataClient().getRemoteServer().updateUser(user);
        			
        			ServerMenuEventListenr.showUserList();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
        		
        	}
        	GuiCommon.showAlertWindow(msg);
        }else if(buttonName.equals(ButtonName.DELETE_USER)){
        	UserListPanel gal=(UserListPanel)svr.getContent();
        	User user=gal.getCurrentUser();
        	SysMessage msg=new SysMessage();
        	msg.subject="User Manangerment";
        	if(user==null){
        		msg.detail="Please select a user first";
        	}else{
        		try {
        			msg=svr.getDataClient().getRemoteServer().deleteUser(user);
        			msg.type=MsgType.Success;
        			msg.detail="User deleted";
        			ServerMenuEventListenr.showUserList();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
        		
        	}
        	GuiCommon.showAlertWindow(msg);
        }else if(buttonName.equals(ButtonName.LEAVE_GROUP)){
        	CommonListener.leaveGroup(svr,this);
        }else if(buttonName.equals(ButtonName.CREATE_ACCOUNT)){
        	CommonListener.createAccount(svr,src);
        }else if(buttonName.equals(ButtonName.ENTER_GROUP)){
        	CommonListener.enterGroup(svr,this);
        }else if(buttonName.equals(ButtonName.ENTER_FORUM)){
        	CommonListener.enterForum(svr,this);
        }else if(buttonName.equals(ButtonName.LEAVE_FORUM)){
        	CommonListener.leaveForum(svr,this);
        }else if(buttonName.equals(ButtonName.CREATE_NEW_GROUP)){
        	CommonListener.createGroup(svr,true);
        }else if(buttonName.equals(ButtonName.CREATE_NEW_FORUM)){
        	CommonListener.createForum(svr,true);
        }else if(buttonName.equals(ButtonName.CREATE_NEW_MESSAGE)){
        	CommonListener.createMeasage(svr,true);
        }else if(buttonName.equals(ButtonName.CREATE_GROUP)){
        	CommonListener.createGroup(svr,src);
        }else if(buttonName.equals(ButtonName.CREATE_FORUM)){
        	CommonListener.createForum(svr,src,this);
        }else if(buttonName.equals(ButtonName.CREATE_MESSAGE)){
        	CommonListener.createMessage(svr,src,this);
        }else if(buttonName.equals(ButtonName.START_CHAT)){
        	CommonListener.startChat(svr);
        }else if(buttonName.equals(ButtonName.DELETE_MESSAGE)){
        	SysMessage msg=new SysMessage();
        	MessageListPanel mlp=(MessageListPanel)svr.getContent();
        	Message message=mlp.getCurrentMessage();
        	if(message==null){
        		msg.detail="Please select a message first";
        	}else{
        		try {
					msg=svr.getDataClient().getRemoteServer().deleteMessage(message);
					msg.detail="Message deleted";
					Forum forum=svr.getDataClient().getCurrentForum();
					List<Message> data=svr.getDataClient().getRemoteServer().getMessageList(forum);
    				MessageListPanel messageListPanel=new MessageListPanel(forum,data,this);
    				if(svr instanceof Server){
    					messageListPanel.showAdminButtons();
    				}
    				svr.setContent(messageListPanel);
				} catch (Exception e2) {
					msg.detail=msg.detail+"\n"+e2.getMessage();
					e2.printStackTrace();
				}
        	}
        	GuiCommon.showAlertWindow(msg);
        }
    }
	}
	
